package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcDeviceType;
import com.soft.idc.common.model.IdcKpiDefine;
import com.soft.idc.common.model.IdcOrg;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.service.IdcDeviceTypeService;
import com.soft.idc.common.util.JQPage;

public class KpiDefineManagerController extends Controller {
	public void index() {
		this.render("/device_manager/kpi_define_manager/kpiDefine.html");
	}
	
	
	public void queryKpiDefine() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		String code = this.getPara("code");
		String name = this.getPara("name");
		String type_id = this.getPara("type_id");
		String is_use = this.getPara("is_use");
		String obj_type = this.getPara("obj_type");
		String tree_type_id = this.getPara("tree_type_id");
		
		sb.append("select df.kpi_id, df.code, df.name as kpi_name, t.name as device_type_name, ");
		sb.append("d.name as device_name, case df.is_use when 1 then '弃用' else '在用' end as is_use_comment, ");
		sb.append("df.value_type, case df.obj_type when 1 then '链路' else '设备' end as obj_type_comment, ");
		sb.append("df.type_id as device_type_id , df.obj_id, df.is_use, df.obj_type, df.bit ");
		
		
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from idc_kpi_define df left join idc_device_type t on df.type_id = t.type_id ");
		sqlFrom.append("left join idc_device d on df.obj_id = d.device_id where 1=1 ");
		
		if(code !=null ){
			sqlFrom.append(" and df.code like '%" + code + "%' ");
		}
		
		if(name !=null ){
			sqlFrom.append(" and df.name like '%" + name + "%' ");
		}
		
		if(type_id !=null ){
			sqlFrom.append(" and df.type_id = " + type_id);
		}
		
		if(is_use !=null ){
			sqlFrom.append(" and df.is_use = " + is_use);
		}
		
		if(obj_type !=null ){
			sqlFrom.append(" and df.obj_type = " + obj_type);
		}
		
		if(tree_type_id != null){
			String typeIds = IdcDeviceTypeService.querySubTypeId(Integer.parseInt(tree_type_id));
			sqlFrom.append(" and df.type_id in (" + typeIds + ")");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}
	
	public void add() {
		IdcKpiDefine idcKpiDefine = new IdcKpiDefine();
		String code = this.getPara("code");
		String name = this.getPara("name");
		String type_id = this.getPara("type_id");
		String obj_id = this.getPara("obj_id");
		String is_use = this.getPara("is_use");
		String value_type = this.getPara("value_type");
		String bit = this.getPara("bit");
		String obj_type = this.getPara("obj_type");
		if(code.length()>0){
			idcKpiDefine.setCode(code);
		}
		
		if(name.length()>0){
			idcKpiDefine.setName(name);
		}
		
		if(type_id.length()>0){
			idcKpiDefine.setTypeId(Integer.parseInt(type_id));
		}
		
		if(obj_id.length()>0){
			idcKpiDefine.setObjId(Integer.parseInt(obj_id));
		}
		
		if(is_use.length()>0){
			idcKpiDefine.setIsUse(Integer.parseInt(is_use));
		}
		
		if(value_type.length()>0){
			idcKpiDefine.setValueType(value_type);
		}
		
		if(bit.length()>0){
			idcKpiDefine.setBit(bit);
		}
		
		if(obj_type.length()>0){
			idcKpiDefine.setObjType(Integer.parseInt(obj_type));
		}
		
		idcKpiDefine.save();
		
		this.renderJson(true);
		
	}
	
	public void delete() {
		final String kpiIds = this.getPara("kpiIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] kpiIdArr = kpiIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from idc_kpi_define where kpi_id in(");
				for(int i=0; i<kpiIdArr.length; i++){
					if(i==kpiIdArr.length-1){
						sb.append(kpiIdArr[i] + ")");
					}else{
						sb.append(kpiIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==kpiIdArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		IdcKpiDefine idcKpiDefine = new IdcKpiDefine();
		String kpi_id = this.getPara("kpi_id");
		String code = this.getPara("code");
		String name = this.getPara("name");
		String type_id = this.getPara("type_id");
		String obj_id = this.getPara("obj_id");
		String is_use = this.getPara("is_use");
		String value_type = this.getPara("value_type");
		String bit = this.getPara("bit");
		String obj_type = this.getPara("obj_type");
		idcKpiDefine.setKpiId(Integer.parseInt(kpi_id));
		if(code.length()>0){
			idcKpiDefine.setCode(code);
		}
		
		if(name.length()>0){
			idcKpiDefine.setName(name);
		}
		
		if(type_id.length()>0){
			idcKpiDefine.setTypeId(Integer.parseInt(type_id));
		}
		
		if(obj_id.length()>0){
			idcKpiDefine.setObjId(Integer.parseInt(obj_id));
		}
		
		if(is_use.length()>0){
			idcKpiDefine.setIsUse(Integer.parseInt(is_use));
		}
		
		if(value_type.length()>0){
			idcKpiDefine.setValueType(value_type);
		}
		
		if(bit.length()>0){
			idcKpiDefine.setBit(bit);
		}
		
		if(obj_type.length()>0){
			idcKpiDefine.setObjType(Integer.parseInt(obj_type));
		}
		boolean result = idcKpiDefine.update();
		
		this.renderJson(result);
	}
	
	public void getTreeData(){
		IdcUser u = this.getSessionAttr("SYS_USER");
		String id = this.getPara("id");
		String name = this.getPara("name");
		List result = new ArrayList();
		//首次加载树
		if(id==null||name==null){
			IdcOrg org = new IdcOrg();
			org = org.findById(u.getOrgId());
			if(org.getParentId()==-1){
				List<IdcOrg> orgList = org.find("select * from idc_org where parent_id = ? ",
						new Object[]{org.getOrgId()});
				//遍历一级节点--组织
				for(IdcOrg orgElement:orgList){
					Map orgNodeMap = new HashMap();
					orgNodeMap.put("id", orgElement.getOrgId() + "_org");
					orgNodeMap.put("pid", "0");
					orgNodeMap.put("name", orgElement.getName());
					orgNodeMap.put("isParent", true);
					result.add(orgNodeMap);
				}
			}else{
				Map orgNodeMap = new HashMap();
				orgNodeMap.put("id", org.getOrgId() + "_org");
				orgNodeMap.put("pid", "0");
				orgNodeMap.put("name", org.getName());
				orgNodeMap.put("isParent", true);
				result.add(orgNodeMap);
			}
		}else{
			if(id.contains("org")){
				String orgId = id.substring(0, id.indexOf("_org"));
				IdcOrg idcOrg = new IdcOrg();
				idcOrg = idcOrg.findById(orgId);
				IdcDeviceType idcDeviceType = new IdcDeviceType();
				List<IdcDeviceType> idcDeviceTypeList = null;
				if(idcOrg.getOrgType()==1){
					//地面设备
					idcDeviceTypeList = idcDeviceType.find("select * from idc_device_Type t where t.is_air = 0 order by type_id asc");
				}else{
					//非地面设备
					idcDeviceTypeList = idcDeviceType.find("select * from idc_device_Type t where t.is_air = 1 order by type_id asc");
				}
				if(idcDeviceTypeList.size()>0){
					for(int i=0; i<idcDeviceTypeList.size(); i++){
						Map deviceTypeMap = new HashMap();
						idcDeviceType = idcDeviceTypeList.get(i);
						if(idcDeviceType.getParentId()==-1){
							deviceTypeMap.put("id", idcDeviceType.getTypeId() + "_type");
							deviceTypeMap.put("pid", id);
							deviceTypeMap.put("name", idcDeviceType.getName());
							deviceTypeMap.put("isParent", true);
							deviceTypeMap.put("open", true);
						}else{
							deviceTypeMap.put("id", idcDeviceType.getTypeId() + "_type");
							deviceTypeMap.put("pid", idcDeviceType.getParentId() + "_type");
							deviceTypeMap.put("name", idcDeviceType.getName());
							deviceTypeMap.put("open", true);
						}
						result.add(deviceTypeMap);
					}
				}
			}
		}
		this.renderJson(result);
	}
}
